package service

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"smart-sensor-cloud/internal/api"
	"smart-sensor-cloud/pkg/util"
)

func GetUnits(ctx *gin.Context) {
	secToi := ctx.Query("sec_toi")
	unitNo := ctx.Query("unit_no")

	client := ctx.MustGet("api").(*api.Client)

	if secToi == "" {
		ctx.Set("error", &util.Error{
			Code:    http.StatusBadRequest,
			Message: "小区数仓ID不可以为空",
		})
		return
	}

	result, err := client.SearchUnits(&api.SearchUnitsReq{
		SectToi: secToi,
		UnitNo:  unitNo,
	})

	if err != nil {
		ctx.Set("error", &util.Error{
			Code:    http.StatusInternalServerError,
			Message: err.Error(),
		})
		return
	}

	util.ResponseOK(ctx, util.OKWithPayload(result.Data.Content))
}
